package com.jrx.anytxn.param.job.partnerShare;

import com.jrx.anytxn.param.config.AnytxnBatchProperties;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrProductTableMapper;
import com.jrx.anytxn.param.service.partner.IPartnerGeneralService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * 分润文件作业
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class PartnerShareProfitConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(PartnerShareProfitConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private IPartnerGeneralService partnerGeneralService;
    @Resource
    private ExtPrProductTableMapper extPrProductTableMapper;

    @Bean
    public Job partnerShareProfitJob() {
        return jobBuilderFactory.get("partnerShareProfitJob")
                .start(partnerShareProfitStep())
                .build();
    }

    @Bean
    public Step partnerShareProfitStep() {
        return stepBuilderFactory.get("partnerShareProfitStep")
                .tasklet(partnerShareProfitTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet partnerShareProfitTasklet() {
        return (stepContribution, chunkContext) -> {
            List<PrProductTable> productTableList = extPrProductTableMapper.findAllForBatch(Constant.STATUS_1);
            ExecutorService service = Executors.newFixedThreadPool(16);
            List<CompletableFuture<Void>> list = productTableList.stream().map(productTable ->
                    CompletableFuture.runAsync(() ->
                            {
                                try {
                                    partnerGeneralService.shareProfit(productTable);
                                } catch (Exception e) {
                                    logger.error("产品分润异常：",e);
                                }
                            }
                            , service)
            ).collect(Collectors.toList());

            long count = list.stream().map(CompletableFuture::join).count();
            logger.info("最终结果数{}",count);
            service.shutdown();
            return RepeatStatus.FINISHED;
        };
    }
}
